//
//  NSString+ChangeHTMLString.m
//  FAKER
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "NSString+ChangeHTMLString.h"
@implementation NSString (ChangeHTMLString)
+ (NSString *)changeHTMLStringWithDataDictionary:(id)data
{
    NSDictionary *dict = [data objectForKey:@"data"];
    NSString *htmlString = [dict objectForKey:@"content"];
    NSArray *mediaArray = [dict objectForKey:@"media"];
    
    if (!mediaArray.count) {
        return htmlString;
    }
    
    htmlString = [htmlString stringByReplacingOccurrencesOfString:[[dict objectForKey:@"js_ref"] firstObject] withString:@""];
    
    NSArray *separatedHTMLStringArray = [htmlString componentsSeparatedByString:@"article_html_content_loading.png"];
    for (NSInteger i = 0; i < separatedHTMLStringArray.count; i++) {
        if (i == 0) {
            htmlString = [NSString stringWithFormat:@"%@%@", separatedHTMLStringArray[i], [mediaArray[i] objectForKey:@"url"]];
            continue;
        }
        if (i == separatedHTMLStringArray.count - 1) {
            htmlString = [NSString stringWithFormat:@"%@%@", htmlString, separatedHTMLStringArray.lastObject];
            break;
        }
        htmlString = [NSString stringWithFormat:@"%@%@%@", htmlString, separatedHTMLStringArray[i], [mediaArray[i] objectForKey:@"url"]];
    }
    
    return htmlString;
}
@end
